<!--
 * @Author: zhangjingyu
 * @Date: 2021-03-16 09:49:44
 * @LastEditors: zhangjingyu
 * @LastEditTime: 2021-03-17 15:03:12
 * @FilePath: \vprisk10.14\src\views\ftpManage\pricingTrial\trialConfiger\trialConfiger.vue
 * @Description: 
-->
<template>
  <!-- 试算配置页面 -->
  <div>
    <el-tabs type="border-card" @tab-click="checkClass">
      <el-tab-pane
        v-for="labelItem in labelList"
        :label="labelItem.text"
        :key="labelItem.id"
      >
        <!-- 操作区域 -->
        <div class="top">
          <!-- 操作按钮区 -->
          <el-button @click="saveFunc()" style="margin-left:30px"
            >保存</el-button
          >
          <el-button @click="getConfigData()">重置</el-button>
          <!-- 右侧试算类型配置下拉 -->
          <el-select
            v-model="testType"
            placeholder="选择试算配置类型"
            class="testTypeSelect"
            @change="getConfigData"
          >
            <el-option
              v-for="(item, i) in testTypeList"
              :key="i"
              :label="item.label"
              :value="item.value"
            ></el-option>
          </el-select>
        </div>
        <!-- 表格组件 -->
        <t-table :data="tableDatas"></t-table>
      </el-tab-pane>
    </el-tabs>
  </div>
</template>

<script>
import { pricingTrial } from "@/api/index";
import tTable from "./component/trialTable";
export default {
  components: { tTable },
  data() {
    return {
      fieldClassify: "1", //试算分类
      testType: "1", // 试算配置类型
      testTypeList: [], // 配置类型字典项
      labelList: [
        { code: "1", text: "基础信息", str: "" },
        { code: "2", text: "利率信息", str: "" },
        { code: "3", text: "期限信息", str: "" },
        { code: "4", text: "支付信息", str: "" },
        { code: "5", text: "重定价信息", str: "" },
        { code: "6", text: "其他信息", str: "" },
        { code: "7", text: "备用字段", str: "" }
      ],
      tableDatas: []
    };
  },
  created() {
    this.getDict();
    this.getConfigData();
  },
  methods: {
    // 获取字典
    getDict() {
      this.$getDict("ftp_pricing_test_config_test_type").then(res => {
        if (res.code == 200) {
          this.testTypeList = res.datas;
        }
      });
    },
    //查询
    getConfigData() {
      let data = {
        fieldClassify: this.fieldClassify,
        testType: this.testType
      };
      pricingTrial.findPTConfig(data).then(res => {
        if (res.code == 200) {
            this.tableDatas = []
            let arr = res.data.testConfigDtoList;
            for (let i = 0; i < arr.length; i++) {
                arr[i].businessEle==null?(arr[i].businessEle={}):''
                arr[i].testConfig==null?(arr[i].testConfig={}):''
            }
          this.tableDatas = arr;
          console.log(arr)
        }
      });
    },
    // 选择标签页回调
    checkClass(val) {
      this.fieldClassify = this.$getLabel(
        val.label,
        this.labelList,
        "text",
        "code"
      );
      this.getConfigData();
    },
    saveFunc() {
      let data = {
        testConfigList: []
      };
      data.testConfigList = this.tableDatas.map(v => {
        return {
          elementField: v.businessEle.elementField,
          fieldClassify: this.fieldClassify,
          fieldConfigLength: v.testConfig.fieldConfigLength,
          inputFlag: v.testConfig.inputFlag,
          showFlag: v.testConfig.showFlag,
          testCaliberCode: v.testConfig.testCaliberCode,
          testType: v.testConfig.testType,
          useCaliber: v.testConfig.useCaliber,
          uuid: v.testConfig.uuid
        };
      });
      console.log(data);
         pricingTrial.testConfigSave(data).then(res=>{
           if (res==true||res.code == 200) {
              this.$message.success("保存成功");
              this.getConfigData();
            } else {
              this.$message.error(res.message || "保存失败");
            }
         })
    }
  }
};
</script>

<style lang="scss" scoped>
.top {
  height: 50px;
}
.testTypeSelect {
  float: right;
  margin-right: 30px;
}
</style>